package com.kede.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kede.dto.AjaxMessage;
import com.kede.dto.LayuiTableData;
import com.kede.entity.*;
import com.kede.service.MenuService;
import com.kede.service.ProductService;
import com.kede.service.ProductTypeService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-02
 */
@Controller
public class ProductController {

    @Autowired
    ProductService productService;
    @Autowired
    MenuService menuService;

    @Autowired
    ProductTypeService productTypeService;
    @GetMapping({"/maf"})
    public String gao(
            String id,
            Model model
    ){
        /*long mid = Long.parseLong(id);
        List<Product> productList=productService.getListByMid(mid);*/
        model.addAttribute("mid",id);
        return "product/gao";
    }
    /**
     * 粮油类页面跳转
     * @param id
     * @param model
     * @return
     */
    @GetMapping({"/lower"})
    public String lower(
            String id,
            Model model
    ){
        /*long mid = Long.parseLong(id);
        List<Product> productList=productService.getListByMid(mid);*/
        model.addAttribute("mid",id);
        return "product/lower";
    }
    @ResponseBody
    @GetMapping("/lowerlist")
    public LayuiTableData lowerlist(
            String id,
            String name,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit
    ){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(name)){
            wrapper.like("name",name);
        }
        if(StringUtils.hasLength(id)){
            wrapper.eq("type",id);
        }
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        wrapper.eq("uid",user.getId());
        wrapper.eq("state",2);
        Page<Product> rolePage = new Page<>();

        rolePage.setCurrent(page);
        rolePage.setSize(limit);

        Page<Product> pageRlt = productService.page(rolePage, wrapper);
        LayuiTableData tableDate = new LayuiTableData();
        List<Product> products = new ArrayList<Product>();
        for (Product product:pageRlt.getRecords()) {
            product.setTypename(productTypeService.getById(product.getType()).getName());
                products.add(product);

        }
        tableDate.setData(products);
        tableDate.setCount(pageRlt.getTotal());
        tableDate.setCode(0);
        return tableDate;
    }

    @ResponseBody
    @GetMapping("/gaolist")
    public LayuiTableData list(
            String id,
            String name,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "limit",defaultValue = "10") Integer limit
    ){
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(name)){
            wrapper.like("name",name);
        }
        if(StringUtils.hasLength(id)){
            wrapper.eq("type",id);
        }
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        wrapper.eq("uid",user.getId());
        wrapper.eq("state",1);
        Page<Product> rolePage = new Page<>();

        rolePage.setCurrent(page);
        rolePage.setSize(limit);

        Page<Product> pageRlt = productService.page(rolePage, wrapper);
        LayuiTableData tableDate = new LayuiTableData();
        List<Product> products = new ArrayList<Product>();
        for (Product product:pageRlt.getRecords()) {
            product.setTypename(productTypeService.getById(product.getType()).getName());
                products.add(product);

        }
        tableDate.setData(products);
        tableDate.setCount(pageRlt.getTotal());
        tableDate.setCode(0);
        return tableDate;
    }

    @ResponseBody
    @PostMapping("/deleteproduct")
    public AjaxMessage deleteproduct(
            String id
    ){
        AjaxMessage message = new AjaxMessage();
        long pid = Long.parseLong(id);
        if (productService.deletByid(pid)){
            message.setCode(0);
        }else {
            message.setCode(1);
            message.setMsg("删除失败");
        }
        return message;
    }

    @GetMapping("/product/update")
    public String update(
            String id,
            Model model
    ){
        long pid = Long.parseLong(id);
        Product product=productService.getById(id);
        product.setTypename(productTypeService.getById(product.getType()).getName());
        model.addAttribute("product",product);
        return "product/update";
    }

    @ResponseBody
    @PostMapping("product/update")
    public AjaxMessage update(
            Product product
    ){
        /*获取当前时间*/
        LocalDateTime nowTime = LocalDateTime.now();
        product.setUpdateTime(nowTime);
        product.setCreateTime(productService.getById(product.getId()).getCreateTime());
        AjaxMessage message = new AjaxMessage();
        if (productService.updateById(product)){
            message.setCode(0);
        }else {
            message.setCode(1);
            message.setMsg("失败");
        }
        return message;
    }
    @ResponseBody
    @PostMapping("/product/typename")
    public AjaxMessage typename(){
        AjaxMessage message = new AjaxMessage();
        QueryWrapper<ProductType> wrapper = new QueryWrapper<ProductType>();
       List<ProductType> menus=productTypeService.list(wrapper);
        if (menus.size()>0){
            message.setCode(0);
            message.setData(menus);
        }else {
            message.setCode(1);
            message.setMsg("失败");
        }
        return message;
    }

    /**
     * 新增商品
     * @return
     */
    @GetMapping("product/add")
    public String add(
            String mid,
            Model model
    ){
        model.addAttribute("mid",mid);
        return "product/add";
    }

    /**
     * 添加数据
     * @param product
     * @return
     */
    @ResponseBody
    @PostMapping("product/add")
    public AjaxMessage add(
            Product product
    ){
        /*获取当前时间*/
        LocalDateTime nowTime = LocalDateTime.now();
        product.setUpdateTime(nowTime);
        product.setCreateTime(nowTime);
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        product.setUid(user.getId());
        AjaxMessage message = new AjaxMessage();
        if (productService.save(product)){
            message.setCode(0);
        }else {
            message.setCode(1);
            message.setMsg("失败");
        }
        return message;
    }
}
